//
//  WBHTTPClient.swift
//  SainMicroblog
//
//  Created by chenxinju on 16/5/4.
//  Copyright © 2016年 chenxinju-a2. All rights reserved.
//

import UIKit
import AFNetworking

//枚举
/**
enum 枚举名称 :类型
*/
enum WBHTTPClientType:String {

  case POST = "POST"
  case GET = "GET"

}


class WBHTTPClient: AFHTTPSessionManager {
    //定义swift 单例
    static let shareInstance: WBHTTPClient = {
        let client = WBHTTPClient()
        client.responseSerializer.acceptableContentTypes?.insert("text/plain")
         return client
    }()
    
    ///  网络下载 GET 和POST
    //override func POST(URLString: String, parameters: AnyObject?, progress uploadProgress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) -> NSURLSessionDataTask? {
      //}

  // override func GET(URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) -> NSURLSessionDataTask? {
     // }

    func  request(type:WBHTTPClientType, URLString: String, parameters: AnyObject?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {

         WBHTTPClient.shareInstance.request(type, URLString: URLString, parameters:parameters, progress: nil, success: success, failure: failure)
    }
    
    //网络请求类封装工具请求 根据判断类型请求的方法类型 请求方法为GET
    func request (type:WBHTTPClientType, URLString: String, parameters: AnyObject?, progress downloadProgress: ((NSProgress) -> Void)?, success: ((NSURLSessionDataTask, AnyObject?) -> Void)?, failure: ((NSURLSessionDataTask?, NSError) -> Void)?) {
         //根据类型类定义请求
        if type == .GET {
         self.GET(URLString, parameters: parameters, progress:downloadProgress, success: success, failure: failure)
        
        }else if type == .POST {
        
          self.POST(URLString, parameters: parameters, progress: downloadProgress, success: success, failure: failure)
        }
    }
}
